package com.user.dao;


import com.user.entity.User;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.query.Query;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
@Scope("prototype")
public class UserDaoImpl implements com.user.dao.UserDao {
    @Autowired
    private SessionFactory sessionFactory = null;


    public User selectOne(String userName) {
        List<User> userList = null;

        Session session = sessionFactory.getCurrentSession();

        Query query = session.createQuery("from User u where u.uName=:ee");
        query.setParameter(0,userName);
        userList = query.getResultList();
        User user = userList.get(0);

        return user;
    }

    public User selectUser(int id) {
        User user = new User();
        Session session = sessionFactory.getCurrentSession();
        user = session.get(User.class,id);
        return user;
    }

    public boolean addOne(User user) {
        boolean flag = false;

       Session session = sessionFactory.getCurrentSession();

            session.save(user);
            flag = true;
            session.getTransaction().commit();

        return flag;
    }

    public boolean deleteOne(int id) {
        boolean flag = false;

        Session session = sessionFactory.getCurrentSession();

            User ss = session.get(User.class,id);
            session.delete(ss);
            flag = true;
            session.getTransaction().commit();

        return flag;
    }

    public boolean updateOne(User user) {
        boolean flag = false;

       Session session = sessionFactory.getCurrentSession();

            User ss = session.get(User.class,user.getuId());
            ss.setuName(user.getuName());
            ss.setuPassword(user.getuPassword());
            session.update(ss);
            session.getTransaction().commit();

        return flag;
    }
}
